#include "ClientCache.h"

const bool CClientCache::Init(const void * pvdPara, char * pszMsgTxt, int iMsgTxtSize)
{
	m_bInitFlg = false;
	m_ptrStrFun = Invoke<IStrFun>("CStrFun", nullptr, nullptr, 0);
	m_ptrClient = Invoke<IClient>("CDataCenterClient", nullptr, nullptr, 0);
	m_bInitFlg = true;
__end:
	return m_bInitFlg;
}

bool CClientCache::LoadRiskInfo(const char * pszLbmNo, const char * pszKey, const char * pszInPara)
{
	bool bRetVal = false;
	shared_ptr<IPacket> ptrReq = Invoke<IPacket>("CPacketJson", pszInPara, nullptr, 0);
	IData * ptrPktResult = nullptr;
	unsigned long ulLen = 0;
	string strResult("");
	if (ptrReq = nullptr)
	{
		goto __end;
	}
	if (m_ptrStrFun->IsEmpty(pszLbmNo))
	{
		goto __end;
	}
	if (m_ptrStrFun->IsEmpty(pszKey))
	{
		goto __end;
	}
	bRetVal = m_ptrClient->SendSyncReqTask("", ptrReq.get(), &ptrPktResult, 60);
	if (!bRetVal)
	{
		goto __end;
	}
	strResult = ptrPktResult->AsString();
	bRetVal = RegisterLbmResult(pszLbmNo, pszKey, strResult.c_str());
__end:
	if (ptrPktResult != nullptr)
	{
		delete ptrPktResult;
		ptrPktResult = nullptr;
	}
	return bRetVal;
}

bool CClientCache::RegisterLbmResult(const char * pszLbmNo, const char * pszKey, const char * pszLbmResult)
{
	bool bRetVal = false;
	string strKey(string(pszLbmNo) + "_" + string(pszKey));
	shared_ptr<ST_DataSet> ptrstDataSet = GetDataSet(pszLbmNo, pszKey);
	if (ptrstDataSet == nullptr)
	{
		ptrstDataSet = NewDataSet(pszLbmNo, pszKey);
		m_mpResult[strKey] = ptrstDataSet;
	}
	try
	{
		ptrstDataSet->ptrLock->Lock();
		ptrstDataSet->ptrData->Clear();
		ptrstDataSet->ptrData->Parse(pszLbmResult);
		bRetVal = true;
	}
	catch (...)
	{

	}
	ptrstDataSet->ptrLock->Unlock();	
__end:
	return bRetVal;
}

const char * CClientCache::GetValueAsString(const char * pszLbmNo, const char * pszKey, const char * pszFieldName, char * pszValue, int iSize, const int iRecNo /*= 1*/)
{
	const char * szRetVal = nullptr;
	shared_ptr<ST_DataSet> ptrstDataSet = GetDataSet(pszLbmNo, pszKey);
	if (ptrstDataSet == nullptr)
	{
		goto __end;
	}
	try
	{
		ptrstDataSet->ptrLock->Lock();
		void * ptrRecInfo = ptrstDataSet->ptrData->ReadRecord(RESULT_SET_NAME, iRecNo);
		if (ptrRecInfo != nullptr)
		{
			const char * szValue = ptrstDataSet->ptrData->GetFieldValueAsString(ptrRecInfo, pszFieldName);
			if (szValue != nullptr)
			{
				m_ptrStrFun->MemCopy((void *)pszValue, (void *)szValue, iSize, strlen(szValue));
				szRetVal = pszValue;
			}
		}
	}
	catch (...)
	{

	}
	ptrstDataSet->ptrLock->Unlock();
__end:
	return szRetVal;
}

long long CClientCache::GetValueAsInt(const char * pszLbmNo, const char * pszKey, const char * pszFieldName, const int iRecNo /*= 1*/)
{
	long long llRetVal = 0;
	shared_ptr<ST_DataSet> ptrstDataSet = GetDataSet(pszLbmNo, pszKey);
	if (ptrstDataSet == nullptr)
	{
		goto __end;
	}
	try
	{
		ptrstDataSet->ptrLock->Lock();
		void * ptrRecInfo = ptrstDataSet->ptrData->ReadRecord(RESULT_SET_NAME, iRecNo);
		if (ptrRecInfo != nullptr)
		{
			llRetVal = ptrstDataSet->ptrData->GetFieldValueAsInt(ptrRecInfo, pszFieldName);
		}
	}
	catch (...)
	{

	}
	ptrstDataSet->ptrLock->Unlock();
__end:
	return llRetVal;
}

double CClientCache::GetValueAsFloat(const char * pszLbmNo, const char * pszKey, const char * pszFieldName, const int iRecNo /*= 1*/)
{
	double dRetVal = 0;
	shared_ptr<ST_DataSet> ptrstDataSet = GetDataSet(pszLbmNo, pszKey);
	if (ptrstDataSet == nullptr)
	{
		goto __end;
	}
	try
	{
		ptrstDataSet->ptrLock->Lock();
		void * ptrRecInfo = ptrstDataSet->ptrData->ReadRecord(RESULT_SET_NAME, iRecNo);
		if (ptrRecInfo != nullptr)
		{
			dRetVal = ptrstDataSet->ptrData->GetFieldValueAsDouble(ptrRecInfo, pszFieldName);
		}
	}
	catch (...)
	{

	}
	ptrstDataSet->ptrLock->Unlock();
__end:
	return dRetVal;
}

bool CClientCache::GetValueAsBool(const char * pszLbmNo, const char * pszKey, const char * pszFieldName, const int iRecNo /*= 1*/)
{
	bool dRetVal = false;
	shared_ptr<ST_DataSet> ptrstDataSet = GetDataSet(pszLbmNo, pszKey);
	if (ptrstDataSet == nullptr)
	{
		goto __end;
	}
	try
	{
		ptrstDataSet->ptrLock->Lock();
		void * ptrRecInfo = ptrstDataSet->ptrData->ReadRecord(RESULT_SET_NAME, iRecNo);
		if (ptrRecInfo != nullptr)
		{
			dRetVal = ptrstDataSet->ptrData->GetFieldValueAsBool(ptrRecInfo, pszFieldName);
		}
	}
	catch (...)
	{

	}
	ptrstDataSet->ptrLock->Unlock();
__end:
	return dRetVal;
}

shared_ptr<ST_DataSet> CClientCache::NewDataSet(const char * pszLbmNo, const char * pszKey)
{
	shared_ptr<ST_DataSet> ptrRetVal = nullptr;		
	ptrRetVal.reset(new ST_DataSet);
	ptrRetVal->ptrLock = Invoke<ILock>("CThreadLock", nullptr, nullptr, 0);
	ptrRetVal->ptrData = Invoke<IPacket>("CPacketJson", nullptr, nullptr, 0);	
__end:
	return ptrRetVal;
}

shared_ptr<ST_DataSet> CClientCache::GetDataSet(const char * pszLbmNo, const char * pszKey)
{
	shared_ptr<ST_DataSet> ptrRetVal = nullptr;
	string strKey(string(pszLbmNo) + "_" + string(pszKey));
	auto it = m_mpResult.find(strKey);
	if (it != m_mpResult.end())
	{
		ptrRetVal = it->second;
	}
	return ptrRetVal;
}


